package com.five.matebook.activity.income;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.icu.util.Calendar;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;

import androidx.annotation.Nullable;

import com.five.matebook.R;
import com.five.matebook.activity.BaseRecordActivity;
import com.five.matebook.activity.RecordForm;
import com.five.matebook.activity.columeedit.CustomValueManager;
import com.five.matebook.activity.db.IncomeRecordTable;
import com.five.matebook.activity.db.OutcomeRecordTable;
import com.five.matebook.activity.outcome.OutcomeColume;
import com.five.matebook.utils.DateUtils;
import com.five.matebook.utils.StringTreeParser;

import java.text.DecimalFormat;
import java.util.List;

public class IncomeRecordActivity extends BaseRecordActivity {
    private static final String TAG = IncomeRecordActivity.class.getSimpleName();

    private StringTreeParser mUsageListParser;

    private Spinner mSpinnerUsageMain;
    private Spinner mSpinnerUsageSecond;
    private EditText mEditMoney;
    private Spinner mSpinnerSpentRole;
    private EditText mEditRemarks;
    private TextView mTxtDateTime;

    private List<StringTreeParser.StringItem> mainUsageList;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        handleIntent(IncomeRecordTable.getInstance(this));

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        setContentView(R.layout.activity_record_income);

        {
            this.mSpinnerUsageMain = findViewById(R.id.spinner_usage_main);
            this.mSpinnerUsageSecond = findViewById(R.id.spinner_usage_sencond);
            this.mTxtDateTime = findViewById(R.id.txt_datetime);
            this.mEditMoney = findViewById(R.id.edit_money);
            this.mSpinnerSpentRole = findViewById(R.id.spent_role);
            this.mEditRemarks = findViewById(R.id.edit_remark);

            setRecordForm(new RecordForm(this, IncomeRecordTable.getInstance(this)));
            this.mRecordForm
                    .bind(IncomeColume.SPENT_MONEY, this.mEditMoney, "消费金额", false, new RecordForm.FormEleTypeTransfer() {
                        @Override
                        public Object getValue(String v) {
                            DecimalFormat decimalFormat = new DecimalFormat("0.00");
                            return Float.valueOf(decimalFormat.format(Float.valueOf(v)));
                        }
                    })
                    .bind(IncomeColume.SPENT_USAGE, this.mSpinnerUsageMain, "消费用途")
                    .bind(IncomeColume.SPENT_SENCOND_USAGE, this.mSpinnerUsageSecond, "消费用途")
                    .bind(IncomeColume.HAPPEND_TIME, this.mTxtDateTime, "消费时间", false, new RecordForm.FormEleTypeTransfer() {
                        @Override
                        public Object getValue(String v) {
                            if (TextUtils.isEmpty(v)) {
                                return Long.valueOf("0");
                            }
                            return getTimeFromTextView(mTxtDateTime);
                        }
                    })
                    .bind(IncomeColume.INCOME_ROLE, this.mSpinnerSpentRole, "消费对象")
                    .bind(IncomeColume.SPENT_REMARKS, this.mEditRemarks, "消费备注", true);
        }

        {
            /*
             ** 初始化： 用途下拉框
             */
            mUsageListParser = getStringTreeParser(CustomValueManager.FILED_USAGE_INCOME, R.array.incomeUsageList, 2);
            mainUsageList = mUsageListParser.getArray(1);

            this.mSpinnerUsageMain.setAdapter(generateSpinnerAdapter(mainUsageList));
            this.mSpinnerUsageMain.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    parent.getAdapter().getItem(position);
                    if (position == 0) {
                        // nothing selected
                        mSpinnerUsageSecond.setAdapter(null);
                    } else {
                        mSpinnerUsageSecond.setAdapter(generateSpinnerAdapter(mUsageListParser.getArray(mainUsageList.get(position))));
                        mSpinnerUsageSecond.performClick();
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });
        }

        {
            // 加载对象列表
            StringTreeParser roleTreeParser = getStringTreeParser(CustomValueManager.FILED_ROLE_INCOME, R.array.spentRole, 1);
            mSpinnerSpentRole.setAdapter(generateSpinnerAdapter(roleTreeParser.getArray(1)));
        }

        {
            /*
             ** 初始化： 日期
             */
            setTime2TextView(mTxtDateTime, DateUtils.getNowTimeLong());
            this.mTxtDateTime.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Calendar calendar = Calendar.getInstance();
                    new DatePickerDialog(IncomeRecordActivity.this, new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                            new TimePickerDialog(IncomeRecordActivity.this, new TimePickerDialog.OnTimeSetListener() {
                                @Override
                                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                                    setTime2TextView(mTxtDateTime, DateUtils.string2Date(year, month, dayOfMonth, hourOfDay, minute).getTime());
                                }
                            }, calendar.get(Calendar.HOUR), calendar.get(Calendar.MINUTE), true).show();
                        }
                    },calendar.get(Calendar.YEAR), // 年份
                            calendar.get(Calendar.MONTH), // 月份
                            calendar.get(Calendar.DAY_OF_MONTH)).show();

                }
            });
        }
        updateUi();

        {
            // 设置下拉框默认选中
            setSpinnerText(mSpinnerSpentRole, 1);
            setSpinnerText(mSpinnerUsageMain, 1);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void updateChildUi() {
        if (mOldData != null) {
            this.mEditMoney.setText(mOldData.get(OutcomeColume.SPENT_MONEY));
            String mainUsage = mOldData.get(OutcomeColume.SPENT_USAGE);
            int index = setSpinnerText(this.mSpinnerUsageMain, mainUsage);
            if (index >= 0) {
                mSpinnerUsageSecond.setAdapter(generateSpinnerAdapter(mUsageListParser.getArray(mainUsageList.get(index))));
            }

            String secondUsage = mOldData.get(OutcomeColume.SPENT_SENCOND_USAGE);
            setSpinnerText(this.mSpinnerUsageSecond, secondUsage);

            setTime2TextView(mTxtDateTime, mOldData.getLong(OutcomeColume.HAPPEND_TIME, 0));
            setSpinnerText(this.mSpinnerSpentRole, mOldData.get(OutcomeColume.SPENT_ROLE));
            this.mEditRemarks.setText(mOldData.get(OutcomeColume.SPENT_REMARKS));
        }

        if (mOldData == null) {
            // 新增模式
            this.mTxtTitle.setText("收入记录~新增");
            this.mButEdit.setVisibility(View.GONE);
            this.mButSubmit.setVisibility(View.VISIBLE);
            this.mButNewRecord.setVisibility(View.GONE);
        } else {
            if (mEditable) {
                // 编辑模式
                this.mTxtTitle.setText("收入记录~编辑");
                this.mButEdit.setVisibility(View.GONE);
                this.mButSubmit.setVisibility(View.VISIBLE);
                this.mButNewRecord.setVisibility(View.GONE);
            } else {
                // 查看模式
                this.mTxtTitle.setText("收入记录");
                this.mButEdit.setVisibility(View.VISIBLE);
                this.mButSubmit.setVisibility(View.GONE);
                this.mButNewRecord.setVisibility(View.VISIBLE);
            }
        }
    }
}
